package fw.qaz.asmx.reader.utils;

import android.content.Context;

import java.text.DecimalFormat;

public abstract class Convertor
{
    public static int dp2px(Context cxt, int dp)
    {
        final float scale = cxt.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static int px2dp(Context cxt, int px)
    {
        final float scale = cxt.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    public static int px2sp(Context cxt, float px)
    {
        final float scale = cxt.getResources().getDisplayMetrics().scaledDensity;
        return (int) (px / scale + 0.5f);
    }

    public static int sp2px(Context cxt, float sp)
    {
        final float scale = cxt.getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * scale + 0.5f);
    }

    private static DecimalFormat df = new DecimalFormat("#.00");

    /**
     * 个性化显示文件大小
     * @param size 文件字节数
     * @return 个性化大小字符串
     */
    public static String individuatedFileSize(int size)
    {
        if (size < 1024)
        {
            return size + "Byte";
        }
        else if (size < 1024 * 1024)
        {
            return df.format(((float) size) / 1024F) + "KiB";
        }
        else if (size < 1024 * 1024 * 1024)
        {
            return df.format(((float) size) / 1024F / 1024F) + "MiB";
        }
        else
        {
            return df.format(((float) size) / 1024F / 1024F / 1024F) + "GiB";
        }
    }
}
